package com.cn.ccp.action.course.selectapproval;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.SelectedCourseBean;
import com.cn.ccp.bean.UserBean;
import com.cn.ccp.util.CCPContants;
import com.cn.ccp.util.Contants;

/**
 * 对学生选课进行审批
 * @author renyongcheng
 *
 */
public class CourseSelectedApprovalAction extends BaseAction {
	private Logger log = Logger.getLogger(CourseSelectedApprovalAction.class);
	private List<SelectedCourseBean>  courseList;//
	private String specialtyId;
	private int approveCount;
	
	public CourseSelectedApprovalAction(){
		log.info("-------CourseReleaseApprovalAction()-------");
		UserBean userBean = getUserService().findById(CCPContants.getUserBean().getId());
		specialtyId = userBean.getSpecialtyId();
		getpagedetail();
	}
 
 
	/**
	 * 
	 * @param agree
	 */
	private void doApproval(boolean agree){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String courseId = varMap.get("id");
		log.info("----"+courseId+":"+agree);
		getSelectCourseService().doApprove(agree ? Contants.SELECTED_COURSE_STATUS_STUDING : Contants.SELECTED_COURSE_STATUS_REJECT, courseId, sdf.format(new Date()));
		getpagedetail();
	}
	
	/**
	 * 同意
	 * @return
	 */
	public String doAgree(){
		log.info("-------doAgree()-------" );
		doApproval(true);
		return MAIN;
	}
	
	/**
	 * 不同意
	 * @return
	 */
	public String doNoAgree(){
		log.info("-------doNoAgree()-------");
		doApproval(false);
		return MAIN;
	}
	
	
	@Override
	public void getpagedetail() {
		approveCount = getSelectCourseService().countSelectApprove(specialtyId);
		setItemCount(approveCount);
		int currentpage = getCurrentPage();
		int pagesize = getPageSize();
		int start = (currentpage - 1) * pagesize;
		courseList = getSelectCourseService().pageQueryForReleaseApproval(specialtyId,start, pagesize);
	}


	public List<SelectedCourseBean> getCourseList() {
		return courseList;
	}


	public void setCourseList(List<SelectedCourseBean> courseList) {
		this.courseList = courseList;
	}


	public String getSpecialtyId() {
		return specialtyId;
	}


	public void setSpecialtyId(String specialtyId) {
		this.specialtyId = specialtyId;
	}


	public int getApproveCount() {
		return approveCount;
	}


	public void setApproveCount(int approveCount) {
		this.approveCount = approveCount;
	}

 
	
	 

	 
   
}
